package com.woniuxy.lrz.carlproject.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.woniuxy.lrz.carlproject.common.util.Results;
import com.woniuxy.lrz.carlproject.controller.form.formgoods.GoodsSkuAOUForm;
import com.woniuxy.lrz.carlproject.service.GoodSkuService;
import com.woniuxy.lrz.carlproject.service.dto.dtogoods.GoodsSkuDto;
import com.woniuxy.lrz.carlproject.service.dto.dtogoods.GoodsSkuPropertyItem;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 商品sku表 前端控制器
 * </p>
 *
 * @author lrz
 * @since 2022-09-28
 */
@RestController
@Api(tags = "商品SKU接口")
@Slf4j
@Validated
@RequestMapping("/carlproject/goodSku")
public class GoodSkuController {

    @Resource
    private GoodSkuService goodSkuService;

    @PostMapping("/addGoodsSku")
    @ApiOperation("新增/修改商品SKU")
    public Results addOrUpdateGoodsSku (@RequestBody @Valid  GoodsSkuAOUForm goodsSkuAOUForms) throws JsonProcessingException {
        log.info("商品SKU:{}",goodsSkuAOUForms);
        goodSkuService.addOrUpdateGoodsSku(goodsSkuAOUForms);
        return Results.instance().setMessage("操作成功");
    }

    @DeleteMapping("/deleteGoodsSku")
    @ApiOperation("删除商品SKU")
    @ApiImplicitParam(name = "skuId",value = "商品SKUId")
    public Results deleteGoodsSku(@NotNull(message = "商品SKUId不能为空") Integer skuId){
        goodSkuService.deleteGoodsSku(skuId);
        return Results.instance().setMessage("删除成功");
    }

    @GetMapping("/findGoodsSku")
    @ApiOperation("加载商品SKU信息")
    @ApiImplicitParam(name = "goodId",value = "商品ID")
    public Results findGoodsSku(@NotNull(message = "商品ID不能为空") Integer goodId) throws JsonProcessingException {
        List<GoodsSkuDto> goodsSku = goodSkuService.findGoodsSku(goodId);
        return Results.instance().setData(goodsSku).setMessage("加载商品SKU信息成功");
    }

    @GetMapping("/loadAllPropertys")
    @ApiOperation("新增SKU时，加载所有的属性信息")
    @ApiImplicitParam(name = "goodCategoryId",value = "分类ID")
    public Results loadAllPropertys(Integer goodCategoryId){
        List<GoodsSkuPropertyItem> goodsSkuPropertyItems = goodSkuService.loadAllPropertys(goodCategoryId);
        return Results.instance().setData(goodsSkuPropertyItems).setMessage("加载所有属性信息成功");
    }

}
